package com.haois.mp.database.module.remote.service.impl;


import com.google.gson.Gson;
import com.haois.mp.database.module.remote.service.IUserService;
import com.haois.mp.database.util.HttpClientUtil;
import com.haois.mp.database.commom.CommonConstants;
import com.haois.mp.database.commom.enums.MpTypeEnum;
import com.haois.mp.database.module.common.daomain.UserToken;
import com.haois.mp.database.module.common.service.IUserTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.HashMap;
import java.util.Map;


@Service("iUserService")
public class UserServiceImpl implements IUserService {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Value("${defined.database.server-url}")
    private String serverUrl;

    @Autowired
    private IUserTokenService iUserTokenService;

    private Gson gson = new Gson();


    @Override
    public Map modifyPwd(String oldPassword, String newPassword, String access_token) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("oldPassword", oldPassword);
        params.put("newPassword", newPassword);
        String url = serverUrl + CommonConstants.USER_MODIFYPWD_URL;
        String response = HttpClientUtil.sendPost(url, params, access_token);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    /*
     * 发送用户注册手机验证码
     */
    @Override
    public Map<String, Object> sendVerifyCode(String phoneNumber) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("phoneNumbers", phoneNumber);
        String url = serverUrl.replace("/app", "") + CommonConstants.USER_SENDCODE;
        String response = HttpClientUtil.sendPost(url, params, null);
        logger.debug("获取用户注册手机验证码返回：{}", response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    @Override
    public Map<String, Object> getVerifyCode(String phoneNumber) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("phoneNumber", phoneNumber);
        String url = serverUrl.replace("/app", "") + CommonConstants.USER_GETCODE;
        String response = HttpClientUtil.sendPost(url, params, null);
        logger.debug("获取用户手机验证码返回：{}", response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    /*
     * 用户注册
     */
    @Override
    public Map register(String username, String password, String vCode) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("memberNum", username);
        params.put("password", password);
        params.put("vCode", vCode);
        String url = serverUrl + CommonConstants.USER_REGISTER_URL;
        String response = HttpClientUtil.sendPost(url, params, null);
        logger.debug("用户注册返回：{}", response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    /*
     * 用户登录
     */
    @Transactional
    @Override
    public synchronized Map login(String username, String password, String openId, String sysType, String sessionKey) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("memberNum", username);
        params.put("password", password);
        String url = serverUrl + CommonConstants.USER_LOGIN_URL;
        String response = HttpClientUtil.sendPost(url, params, null);
        Map result = gson.fromJson(response, Map.class);
        logger.debug("登录返回结果:{}", response);
        return result;
    }

    @Override
    public Map<String, String> getUserInfo(String access_token, String memberNum, String oid) {
        Map params = new HashMap();
        params.put("oid", oid);
        String url = serverUrl + CommonConstants.USER_INFO_URL + "/" + memberNum;
        String response = HttpClientUtil.sendGet(url, params, access_token);
        logger.debug("用户{}的信息为：{}", memberNum, response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    @Override
    public Map getUserByName(String access_token, String name) {
        Map map = new HashMap();
        map.put("name", name);
        String url = serverUrl + CommonConstants.INFO_QUICK_NAME_URL;
        String response = HttpClientUtil.sendPost(url, map, access_token);

        logger.debug("通过名称快速查询结果为：{}", response);
        Map result = gson.fromJson(response, Map.class);

        return result;
    }

    @Override
    public Map<String, String> getUserInfo(String access_token, String oid) {
        Map map = new HashMap();
        map.put("oid", oid);
        String url = serverUrl + CommonConstants.GROUP_MEM.replace(CommonConstants.OID, oid);
        String response = HttpClientUtil.sendGet(url, map, access_token);
        logger.debug("用户和组织关联信息为：{}", response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    @Override
    public Map<String, String> getUserInfo(String access_token) {
        String url = serverUrl + CommonConstants.USER_INFO_URL;
        String response = HttpClientUtil.sendGet(url, new HashMap(), access_token);
        logger.debug("用户个人信息为：{}", response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    @Override
    public Map<String, String> getUserState(String access_token) {
        String url = serverUrl + CommonConstants.USER_INFO_MSG_URL;
        String response = HttpClientUtil.sendGet(url, new HashMap(), access_token);
        logger.debug("用户个人信息状态为：{}", response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    @Override
    public Map updateUserInfo(Map params, String access_token) {
        String url = serverUrl + CommonConstants.USER_INFO_URL;
        String response = HttpClientUtil.sendPost(url, params, access_token);
        logger.debug("更新用户信息返回：{}", response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }


    @Override
    public Map modifyPhoto(File photo, String access_token) {
        UserToken userToken = iUserTokenService.findByAccessToken(access_token, MpTypeEnum.OFFACCOUNTS.getType());
        Map params = new HashMap();
        params.put("memberNum", userToken.getUserId());
        Map files = new HashMap();
        files.put("picture", photo);
        String url = serverUrl + CommonConstants.USER_PHOTO;
        String respStr = HttpClientUtil.sendPost(url, params, access_token, files);
        logger.info("上传信息返回结果为:{}", respStr);
        Map map = gson.fromJson(respStr, Map.class);
        return map;
    }

    @Override
    public Map resetUserPassword(String access_token, String code, String newPassword, String phoneNumber) {
        Map params = new HashMap();
        params.put("phoneNumber", phoneNumber);
        params.put("code", code);
        params.put("newPassword", newPassword);
        String url = serverUrl + CommonConstants.USER_FORGETPWD_URL;
        String response = HttpClientUtil.sendPost(url, params, null);
        logger.debug("重置密码返回：{}", response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    @Override
    public Map sendResetPassVerifyCode(String phoneNumber, String access_token) {
        Map params = new HashMap();
        params.put("phoneNumbers", phoneNumber);
        String url = serverUrl.replace("/app", "") + CommonConstants.USER_SENDCODE;
        String response = HttpClientUtil.sendGet(url, params, access_token);
        logger.debug("发送重置验证码返回：{}", response);
        Map result = gson.fromJson(response, Map.class);
        return result;
    }

    @Override
    public boolean loginApi(String userName, String passWord) {
        return true;
    }

}